/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exatrez.stripes.action;

import able.stripes.JSP;
import com.google.inject.Inject;
import exatrez.dao.DepartmentDAO;
import exatrez.dao.RefShipVisitHeaderDAO;
import exatrez.dao.RefShipVisitMatchDAO;
import exatrez.dao.RefShipVisitSubDAO;
import exatrez.manager.ShipVisit3Manager;
import exatrez.model.Department;
import exatrez.model.InfoAudit;
import exatrez.model.RefShipVisitHeader;
import exatrez.model.RefShipVisitMatch;
import exatrez.model.RefShipVisitMatchSub;
import exatrez.model.RefShipVisitSub;
import exatrez.model.User;
import exatrez.stripes.BaseActionBean;
import exatrez.stripes.Configuration;
import exatrez.stripes.ShipVisitConfiguration;
import exatrez.stripes.config.AuthRequired;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Mansur
 */
@UrlBinding("/shipVisit3Admin")
@AuthRequired
public class ShipVisit3AdminAction extends BaseActionBean{
    
    private static Logger LOG = LoggerFactory.getLogger(ShipVisit3AdminAction.class);
    
    @Inject
    ShipVisitConfiguration svconf;
    @Inject
    Configuration conf;
    @Inject
    private ShipVisit3Manager sv3Manager;
    @Inject
    private RefShipVisitHeaderDAO refHeaderDAO;
    @Inject
    private RefShipVisitSubDAO refSubHeaderDAO;
    @Inject
    private RefShipVisitMatchDAO matchDAO;
    @Inject
    private DepartmentDAO departmentDAO;
    
    private static final String VIEWHEADER = "admin/sv3_main_header.jsp";
    private static final String EDITHEADER = "admin/sv3_newEdit_main_header.jsp";
    private static final String VIEWSUBHEADER = "admin/sv3_sub_header.jsp";
    private static final String EDITSUBHEADER = "admin/sv3_newEdit_sub_header.jsp";
    private static final String VIEWMATCH = "admin/sv3_main_match.jsp";
    private static final String EDITMATCH = "admin/sv3_editM_match.jsp";
    private static final String VIEWSUBMATCH = "admin/sv3_sub_match.jsp";
    private static final String EDITSUBMATCH = "admin/sv3_editS_match.jsp";
    
    private String headerId;
    private String subId;
    private String departmentId;
    
    private RefShipVisitHeader refHeader;
    private RefShipVisitSub refSubHeader;
    private RefShipVisitMatch refMatch;
    private Department department;
    
    private List<RefShipVisitHeader> listHeader;
    private List<RefShipVisitSub> listSubHeader;
    private List<RefShipVisitMatch> listMatch;
    private List<String> listOrder;
    private List<String> listHeaderSelect;
    private List<String> listSubHeaderSelect;
    
    @DefaultHandler
    public Resolution viewHeader(){
        LOG.debug("method viewHeader start");
        listHeader = refHeaderDAO.findAll();
        LOG.debug("listHeader.size :"+listHeader.size());
        LOG.debug("method viewHeader finish");
        return new JSP(VIEWHEADER);
    }
    
    public Resolution newEditHeader(){
        headerId = getContext().getRequest().getParameter("id");
        listOrder = new ArrayList<String>();
        if(StringUtils.isNotBlank(headerId)){
            refHeader = refHeaderDAO.findById(Integer.parseInt(headerId));
        }
        
        for (RefShipVisitHeader h : refHeaderDAO.findAll()) {
            listOrder.add(h.getOrderFlag()+"");
        }
        LOG.debug("listOrder.size :"+listOrder.size());
        return new JSP(EDITHEADER).addParameter("popup", true);
    }
    
    public Resolution refreshHeader(){
        return new RedirectResolution(ShipVisit3AdminAction.class,"viewHeader");
    }
    
    public Resolution saveUpdateHeader(){
        User user = getContext().getCurrentUser();
        RefShipVisitHeader header = null;
        InfoAudit ia = null;
        try{
            if(StringUtils.isNotBlank(headerId)){
                header = refHeaderDAO.findById(Integer.parseInt(headerId));
                ia = header.getInfoAudit();
                ia.setUpdateId(user);
                ia.setUpdateDate(new Date());
            }else{
                header = new RefShipVisitHeader();
                ia = new InfoAudit();
                ia.setCreateId(user);
                ia.setCreateDate(new Date());
            }

            header.setActiveFlag(refHeader.getActiveFlag());
            header.setDescription(refHeader.getDescription());
            header.setInputType(refHeader.getInputType());
            header.setOrderFlag(refHeader.getOrderFlag());
            header.setInfoAudit(ia);

            String result = sv3Manager.saveorUpdateRefHeader(header);
            if(result.equals("success")){
                addSimpleMessage(svconf.getProperty("success"));
            }else{
                addSimpleError(svconf.getProperty("form.error"));
            }
        }catch(Exception ex){
            LOG.error(ex.getMessage());
            ex.printStackTrace();//for development use only
            addSimpleError(svconf.getProperty("form.error"));
        }
        
        return new RedirectResolution(ShipVisit3AdminAction.class,"viewHeader");
    }
    
    public Resolution viewSubHeader(){
        LOG.debug("method viewSubHeader start");
        listSubHeader = refSubHeaderDAO.findAll();
        LOG.debug("listSubHeader.size :"+listSubHeader.size());
        LOG.debug("method viewSubHeader finish");
        return new JSP(VIEWSUBHEADER);
    }
    
    public Resolution newEditSubHeader(){
        subId = getContext().getRequest().getParameter("id");
        listOrder = new ArrayList<String>();
        if(StringUtils.isNotBlank(subId)){
            refSubHeader = refSubHeaderDAO.findById(Integer.parseInt(subId));
        }
        
        for (RefShipVisitSub h : refSubHeaderDAO.findAll()) {
            listOrder.add(h.getOrderFlag()+"");
        }
        LOG.debug("listOrder.size :"+listOrder.size());
        return new JSP(EDITSUBHEADER).addParameter("popup", true);
    }
    
    public Resolution refreshSubHeader(){
        return new RedirectResolution(ShipVisit3AdminAction.class,"viewSubHeader");
    }
    
    public Resolution saveUpdateSubHeader(){
        User user = getContext().getCurrentUser();
        RefShipVisitSub sub = null;
        InfoAudit ia = null;
        try{
            if(StringUtils.isNotBlank(subId)){
                sub = refSubHeaderDAO.findById(Integer.parseInt(subId));
                ia = sub.getInfoAudit();
                ia.setUpdateId(user);
                ia.setUpdateDate(new Date());
            }else{
                sub = new RefShipVisitSub();
                ia = new InfoAudit();
                ia.setCreateId(user);
                ia.setCreateDate(new Date());
            }

            sub.setActiveFlag(refSubHeader.getActiveFlag());
            sub.setDescription(refSubHeader.getDescription());
            sub.setInputType(refSubHeader.getInputType());
            sub.setOrderFlag(refSubHeader.getOrderFlag());
            sub.setInfoAudit(ia);

            String result = sv3Manager.saveorUpdateRefSubHeader(sub);
            if(result.equals("success")){
                addSimpleMessage(svconf.getProperty("success"));
            }else{
                addSimpleError(svconf.getProperty("form.error"));
            }
        }catch(Exception ex){
            LOG.error(ex.getMessage());
            ex.printStackTrace();//for development use only
            addSimpleError(svconf.getProperty("form.error"));
        }
        
        return new RedirectResolution(ShipVisit3AdminAction.class,"viewSubHeader");
    }
    
    public Resolution viewMatch(){
        LOG.debug("method viewMatch start");
        listMatch = sv3Manager.getListShipVisitMatch(null);
        LOG.debug("listMatch.size :"+listMatch.size());
        LOG.debug("method viewMatch finish");
        return new JSP(VIEWMATCH);
    }
    
    public Resolution editMatch(){
        departmentId = getContext().getRequest().getParameter("id");
        listHeaderSelect = new ArrayList<String>();
        if(StringUtils.isNotBlank(departmentId)){
            department = departmentDAO.findById(Integer.parseInt(departmentId));
            listMatch = sv3Manager.getListShipVisitMatch(Integer.parseInt(departmentId));
            for (RefShipVisitMatch m : listMatch) {
                listHeaderSelect.add(m.getShipVisitHeader().getRefShipVisitHeaderId().toString());
            }
        }
        LOG.debug("listHeaderSelect.size :"+listHeaderSelect.size());
        return new JSP(EDITMATCH).addParameter("popup", true);
    }
    
    public Resolution refreshMatch(){
        return new RedirectResolution(ShipVisit3AdminAction.class,"viewMatch");
    }
    
    public Resolution saveUpdateMatch(){
        User user = getContext().getCurrentUser();
        LOG.debug("listHeaderSelect.size :"+listHeaderSelect.size());
        LOG.debug("departmentId :"+departmentId);
        try{
            List<RefShipVisitMatch> l = sv3Manager.getListShipVisitMatch(Integer.parseInt(departmentId));
            LOG.debug("l.size :"+l.size());
            List<String> listAdd = new ArrayList<String>();
            for (String hs : listHeaderSelect) {
                LOG.debug("items :"+hs.toString());
                boolean notHave = true;
                for (RefShipVisitMatch m : l) {
                    LOG.debug("refHeaderId :"+m.getShipVisitHeader().getRefShipVisitHeaderId());
                    if(m.getShipVisitHeader().getRefShipVisitHeaderId() == Integer.parseInt(hs)){
                        LOG.debug("Match");
                        notHave = false;
                        l.remove(m);
                        LOG.debug("l.size :"+l.size());
                        break;
                    }
                }
                if(notHave){
                    listAdd.add(hs);
                }
            }
            LOG.debug("final ==> l.size :"+l.size());
            LOG.debug("final ==> listAdd.size :"+listAdd.size());
            String result = sv3Manager.saveorUpdateMatchHeader(l, listAdd, user, departmentId);
            if(result.equals("success")){
                addSimpleMessage(svconf.getProperty("success"));
            }else{
                addSimpleError(svconf.getProperty("form.error"));
            }
        }catch(Exception ex){
            LOG.error(ex.getMessage());
            ex.printStackTrace();//for development use only
            addSimpleError(svconf.getProperty("form.error"));
        }
        return new RedirectResolution(ShipVisit3AdminAction.class,"viewMatch");
    }
    
    public Resolution viewSubMatch(){
        LOG.debug("method viewSubMatch start");
        listMatch = sv3Manager.getListShipVisitMatch(null);
        LOG.debug("listMatch.size :"+listMatch.size());
        LOG.debug("method viewSubMatch finish");
        return new JSP(VIEWSUBMATCH);
    }
    
    public Resolution editSubMatch(){
        departmentId = getContext().getRequest().getParameter("deptId");
        headerId = getContext().getRequest().getParameter("headId");
        listSubHeaderSelect = new ArrayList<String>();
        listMatch = new ArrayList<RefShipVisitMatch>();
        if(StringUtils.isNotBlank(departmentId)){
            department = departmentDAO.findById(Integer.parseInt(departmentId));
            listMatch = sv3Manager.getListShipVisitMatch(Integer.parseInt(departmentId));
            for (RefShipVisitMatch m : listMatch) {
                for (RefShipVisitMatchSub ms : m.getListMatchSub()) {
                    listSubHeaderSelect.add(ms.getShipVisitSub().getRefShipVisitSubId().toString());
                }
            }
            if(StringUtils.isNotBlank(headerId)){
                refHeader = refHeaderDAO.findById(Integer.parseInt(headerId));
                LOG.debug("refHeader :"+refHeader.getRefShipVisitHeaderId());
            }
        }
        LOG.debug("headerId :"+headerId);
        LOG.debug("listMatch.size :"+listMatch.size());
        LOG.debug("listSubHeaderSelect.size :"+listSubHeaderSelect.size());
        return new JSP(EDITSUBMATCH).addParameter("popup", true);
    }
    
    public Resolution saveUpdateSubMatch(){
        User user = getContext().getCurrentUser();
        LOG.debug("headerId :"+headerId);
        LOG.debug("listSubHeaderSelect.size :"+listSubHeaderSelect.size());
        LOG.debug("refHeader.id :"+refHeader.getRefShipVisitHeaderId());
        LOG.debug("departmentId :"+departmentId);
        try{
            List<RefShipVisitMatch> listM = sv3Manager.getListShipVisitMatch(Integer.parseInt(departmentId));
            List<RefShipVisitMatchSub> listRemove = new ArrayList<RefShipVisitMatchSub>();
            if(refHeader.getRefShipVisitHeaderId().toString().equals(headerId)){
                for (RefShipVisitMatch rsvm : listM) {
                    if(rsvm.getListAllMatchSub().size() > 0){
                        for (RefShipVisitMatchSub rsvms : rsvm.getListAllMatchSub()) {
                            boolean notHave = true;
                            for (String s : listSubHeaderSelect) {
                                if(rsvms.getShipVisitSub().getRefShipVisitSubId().toString().equals(s.toString())){
                                    LOG.debug("match :"+s);
                                    notHave = false;
                                    listSubHeaderSelect.remove(s);
                                    LOG.debug("listSubHeaderMatch.size :"+listSubHeaderSelect.size());
                                    break;
                                }
                            }
                            if(notHave){
                                listRemove.add(rsvms);
                            }
                        }
                        break;
                    }
                }
            }else{
                for (RefShipVisitMatch m : listM) {
                    if(m.getListAllMatchSub().size() > 0){
                        listRemove = m.getListAllMatchSub();
                        break;
                    }
                }
            }
            
            LOG.debug("finale ==> listRemove.size :"+listRemove.size());
            LOG.debug("finale ==> listSubHeaderSelect.size :"+listSubHeaderSelect.size());
            
            RefShipVisitMatch parent = sv3Manager.getRefMatch(departmentId, refHeader.getRefShipVisitHeaderId().toString());
            
            LOG.debug("parent :"+parent.getRefShipVisitMatchId());
            
            String result = sv3Manager.saveUpdateSubMatch(listRemove, listSubHeaderSelect, parent, user);
            if(result.equals("success")){
                addSimpleMessage(svconf.getProperty("success"));
            }else{
                addSimpleError(svconf.getProperty("form.error"));
            }
            
        }catch(Exception ex){
            LOG.error(ex.getMessage());
            ex.printStackTrace();//for development use only
            addSimpleError(svconf.getProperty("form.error"));
        }
        
        return new RedirectResolution(ShipVisit3AdminAction.class,"viewSubMatch");
    }

    public List<RefShipVisitHeader> getListHeader() {
        return listHeader;
    }

    public void setListHeader(List<RefShipVisitHeader> listHeader) {
        this.listHeader = listHeader;
    }

    public String getHeaderId() {
        return headerId;
    }

    public void setHeaderId(String headerId) {
        this.headerId = headerId;
    }

    public String getSubId() {
        return subId;
    }

    public void setSubId(String subId) {
        this.subId = subId;
    }

    public RefShipVisitHeader getRefHeader() {
        return refHeader;
    }

    public void setRefHeader(RefShipVisitHeader refHeader) {
        this.refHeader = refHeader;
    }

    public List<String> getListOrder() {
        return listOrder;
    }

    public void setListOrder(List<String> listOrder) {
        this.listOrder = listOrder;
    }

    public List<RefShipVisitSub> getListSubHeader() {
        return listSubHeader;
    }

    public void setListSubHeader(List<RefShipVisitSub> listSubHeader) {
        this.listSubHeader = listSubHeader;
    }

    public RefShipVisitSub getRefSubHeader() {
        return refSubHeader;
    }

    public void setRefSubHeader(RefShipVisitSub refSubHeader) {
        this.refSubHeader = refSubHeader;
    }

    public String getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(String departmentId) {
        this.departmentId = departmentId;
    }

    public RefShipVisitMatch getRefMatch() {
        return refMatch;
    }

    public void setRefMatch(RefShipVisitMatch refMatch) {
        this.refMatch = refMatch;
    }

    public List<RefShipVisitMatch> getListMatch() {
        return listMatch;
    }

    public void setListMatch(List<RefShipVisitMatch> listMatch) {
        this.listMatch = listMatch;
    }

    public List<String> getListHeaderSelect() {
        return listHeaderSelect;
    }

    public void setListHeaderSelect(List<String> listHeaderSelect) {
        this.listHeaderSelect = listHeaderSelect;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    public List<String> getListSubHeaderSelect() {
        return listSubHeaderSelect;
    }

    public void setListSubHeaderSelect(List<String> listSubHeaderSelect) {
        this.listSubHeaderSelect = listSubHeaderSelect;
    }
    
}
